﻿Public Class FrmAdmin
    Public ds As DataSet = New DataSet
    Public ds1 As DataSet = New DataSet
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If InvokeRequired Then
            Dim eventArgs() As Object = {sender, e}
            Invoke(New EventHandler(AddressOf OK_Click), _
              eventArgs)
            Return
        End If
        'OK.Enabled = False
        Try
            '    Dim name As String = cmbUsername.SelectedItem.ToString
            '    Dim sql As String = "select UPwd,UPriority from userInfo where UName=" & "'" & name & "'"
            '    Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= Tool\user.accdb;Persist Security Info=False;Jet OLEDB:Database Password='1234'"
            '    ds1 = SearchDB(sql, cnStr)
            '    If ds1.Tables(0).Rows.Count > 0 Then
            '        If PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 1 Then
            '            gPriority = 1
            '        ElseIf PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 2 Then
            '            gPriority = 2
            '        ElseIf PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 3 Then
            '            gPriority = 3
            '        Else
            '            MsgBox("用户名或密码错误,请重新输入!")
            '            gPriority = 0
            '        End If
            '        If gPriority = 0 Then
            '            DataByPCS.OperatorID = ""
            '        Else
            '            DataByPCS.OperatorID = cmbUsername.Text
            '        End If

            '    End If
            '       gPriority = 1
            'gEngineerPwd = GetSetting(My.Application.Info.Title, "Setting", "Engineer", "123")
            'gAdminPwd = GetSetting(My.Application.Info.Title, "Setting", "Admin", "LNLON")

            'Select Case cmbUsername.Text
            '    Case "Operator"

            '    Case "Engineer"
            '        If PasswordTextBox.Text = gEngineerPwd Then
            '            gPriority = 1
            '            FrmMain.TextBox_OPID.Text = "Engineer"
            '            FrmMain.TextBox_OPID.BackColor = Color.Lime
            '        Else
            '            MsgBox("用户名或密码错误,请重新输入! ")
            '            gPriority = 0
            '        End If
            '    Case "Admin"
            '        If PasswordTextBox.Text = gAdminPwd Then
            '            gPriority = 2
            '            FrmMain.TextBox_OPID.Text = "Admin"
            '            FrmMain.TextBox_OPID.BackColor = Color.Lime
            '        Else
            '            MsgBox("用户名或密码错误,请重新输入! ")
            '            gPriority = 0
            '        End If
            '    Case Else
            '        MsgBox("用户名或密码错误,请重新输入! ")
            '        Me.ShowDialog()
            'End Select
            Dim name As String = cmbUsername.SelectedItem.ToString
            Dim sql As String = "select UPwd,UPriority from userInfo where UName=" & "'" & name & "'"
            ds1 = SearchMysql(sql)
            If ds1.Tables(0).Rows.Count > 0 Then
                If PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 1 Then
                    gPriority = 1

                ElseIf PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 2 Then
                    gPriority = 2
                ElseIf PasswordTextBox.Text = ds1.Tables(0).Rows(0)(0) And ds1.Tables(0).Rows(0)(1) = 3 Then
                    gPriority = 3
                Else
                    MsgBox("用户名或密码错误,请重新输入!")
                    gPriority = 0
                End If
                If gPriority = 0 Then

                    DataByPCS.OperatorID = ""
                Else

                    DataByPCS.OperatorID = cmbUsername.Text
                End If

            End If
            EnableMenu()
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub FrmAdmin_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub FrmAdmin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' If cmbUsername.Items.Count = 0 Then
        '    cmbUsername.Items.Add("Engineer")
        '    cmbUsername.Items.Add("Admin")
        'End If
        Dim sql As String = "select * from userInfo"
        ds = SearchMysql(sql)
        cmbUsername.Items.Clear()
        If cmbUsername.Items.Count = 0 Then
            If ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    cmbUsername.Items.Add(ds.Tables(0).Rows(i)(1))
                Next
            End If
        End If
        cmbUsername.SelectedIndex = 0
        PasswordTextBox.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            'If MsgBox("Reset Password as Before !", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            '    SaveSetting(My.Application.Info.Title, "Setting", "Engineer", "123")
            '    SaveSetting(My.Application.Info.Title, "Setting", "Admin", "XC")
            'Else
            '    Exit Sub
            'End If
            If MsgBox("确定删除" & cmbUsername.SelectedItem.ToString & "此账户吗？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Dim name As String = cmbUsername.SelectedItem.ToString
                Dim sql As String = "select UPwd,UPriority from userInfo where UName=" & "'" & name & "'"
                ds1 = SearchMysql(sql)
                If ds1.Tables(0).Rows.Count > 0 Then
                    Dim sql1 As String = "delete from userInfo where UName=" & "'" & cmbUsername.SelectedItem.ToString & "'"
                    HandleMysql(sql1)
                End If
            Else
                Exit Sub
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub EnableMenu()
        Try
            Select Case gPriority
                Case 0
                    FrmProduct.ToolStrip1.Enabled = False
                    FrmProduct.ChToolStripMenuItem.Enabled = False
                    'FrmMain.ToolStripButton17.Enabled = False
                    FrmMain.产品管理ProductToolStripMenuItem.Enabled = False
                    FrmMain.设置ToolStripMenuItem.Enabled = False
                    FrmMain.参数设置ToolStripMenuItem.Enabled = False
                    FrmMain.故障处理设置ToolStripMenuItem.Enabled = False
                    FrmMain.Panel1.Enabled = False
                    FrmMain.Button4.Enabled = False
                    FrmMain.Count_Clear.Enabled = False

                    FrmMain.ToolStripButton17.Enabled = False
                    FrmMain.注册RegisteToolStripMenuItem.Enabled = False
                    FrmMain.TCmbPro.Enabled = False
                    FrmMain.TCmbPro.Enabled = False
                    FrmMain.ToolStripButton17.Enabled = False

                    'FrmMain.TestToolStripMenuItem.Enabled = False
                    'FrmMain.TxtScrewIndex1.ReadOnly = False
                Case 1
                    FrmProduct.ToolStrip1.Enabled = True
                    FrmProduct.ChToolStripMenuItem.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True
                    FrmMain.产品管理ProductToolStripMenuItem.Enabled = True
                    FrmMain.设置ToolStripMenuItem.Enabled = True
                    FrmMain.参数设置ToolStripMenuItem.Enabled = True
                    FrmMain.故障处理设置ToolStripMenuItem.Enabled = True
                    FrmMain.Panel1.Enabled = True
                    FrmMain.Button4.Enabled = True
                    FrmMain.Count_Clear.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True
                    FrmMain.注册RegisteToolStripMenuItem.Enabled = True
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True

                    FrmMain.扫码管理ToolStripMenuItem.Enabled = False
                    FrmMain.设备信息设置ToolStripMenuItem.Enabled = False

                    'FrmMain.TestToolStripMenuItem.Enabled = True
                    'FrmMain.TxtScrewIndex1.ReadOnly = True
                Case 2
                    FrmProduct.ToolStrip1.Enabled = True
                    FrmProduct.ChToolStripMenuItem.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True
                    FrmMain.产品管理ProductToolStripMenuItem.Enabled = True
                    FrmMain.设置ToolStripMenuItem.Enabled = True
                    FrmMain.参数设置ToolStripMenuItem.Enabled = True
                    FrmMain.故障处理设置ToolStripMenuItem.Enabled = True
                    FrmMain.Panel1.Enabled = True
                    FrmMain.Button4.Enabled = True
                    FrmMain.Count_Clear.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True
                    FrmMain.注册RegisteToolStripMenuItem.Enabled = True
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True

                    FrmMain.扫码管理ToolStripMenuItem.Enabled = True
                    FrmMain.设备信息设置ToolStripMenuItem.Enabled = True
                    'FrmMain.TestToolStripMenuItem.Enabled = True
                    'FrmMain.TxtScrewIndex1.ReadOnly = True

                Case 3
                    FrmProduct.ToolStrip1.Enabled = False
                    FrmProduct.ChToolStripMenuItem.Enabled = False
                    'FrmMain.ToolStripButton17.Enabled = False
                    FrmMain.产品管理ProductToolStripMenuItem.Enabled = False
                    FrmMain.设置ToolStripMenuItem.Enabled = False
                    FrmMain.参数设置ToolStripMenuItem.Enabled = False
                    FrmMain.故障处理设置ToolStripMenuItem.Enabled = False
                    FrmMain.Panel1.Enabled = False
                    FrmMain.Button4.Enabled = False
                    FrmMain.Count_Clear.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True
                    FrmMain.注册RegisteToolStripMenuItem.Enabled = False
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.TCmbPro.Enabled = True
                    FrmMain.ToolStripButton17.Enabled = True

            End Select
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FrmPwdChange.ShowDialog()
    End Sub

End Class
